package cn.gok.mybatisplus22.controller;

import cn.gok.mybatisplus22.entity.Clazz;
import cn.gok.mybatisplus22.entity.Student;
import cn.gok.mybatisplus22.mapper.ClazzMapper;
import cn.gok.mybatisplus22.mapper.StudentMapper;
import cn.gok.mybatisplus22.service.ClazzService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ClazzController
 * @Description TODO
 * @Author HP
 * @Date 9:43 2022/7/22
 * @Version 1.0
 **/
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    //班级功能模块
    //查询班级所有信息
    @Autowired
    private ClazzMapper clazzMapper;
    @RequestMapping("/findAll")
    public List<Clazz>findAllClazz(){
        return clazzService.list(null);
    }
    //根据id查询班级
    @PostMapping("/findById")
    public Clazz findByIdClazz(@RequestParam Long id){//上传参数
        return clazzService.getById(id);
    }
    //增加班级的信息
    @RequestMapping("/save")
    public boolean saveClazz(@RequestBody Clazz clazz){//上传整个类
        return clazzService.save(clazz);
    }
    //通过id修改班级信息
    @RequestMapping("/update")
    public boolean updateClazz(@RequestBody Clazz clazz){
        return clazzService.updateById(clazz);
    }
    //通过id删除班级信息
    @RequestMapping("/remove")
    public boolean removeClazz(@RequestParam Long id){
        return clazzService.removeById(id);
    }
    //批量删除班级的信息
    @RequestMapping("/removeByIds")
    public boolean removeByIdsClazz(@RequestParam List<String> ids){
        return clazzService.removeByIds(ids);
    }
    //查询所有班级信息和对应学生信息
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/selectallstudent")
    public Map<Clazz,List<Student>> selectallstudent(){
        Map<Clazz,List<Student>> map=new HashMap<>();
        List<Clazz> clazzes=clazzMapper.selectList(null);
        for (Clazz clazz : clazzes) {
            QueryWrapper<Student>wrapper=new QueryWrapper<>();
            wrapper.eq("cid",clazz.getCid());
            List<Student> students=studentMapper.selectList(wrapper);
            map.put(clazz,students);
        }
        return map;
    }



}
